package com.pai4j.user.repository.dao;

import com.pai4j.user.repository.entity.GroupInvitationEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
public interface IGroupInvitationDAO extends JpaSpecificationExecutor<GroupInvitationEntity>, JpaRepository<GroupInvitationEntity, Long> {

    /**
     * 根据群ID、邀请人和被邀请人查找邀请记录
     */
    GroupInvitationEntity findByGroupPaiIdAndInviterAccountAndInviteeAccount(String groupPaiId, String inviterAccount, String inviteeAccount);

    /**
     * 根据邀请码查找邀请记录
     */
    GroupInvitationEntity findByInvitationCode(String invitationCode);

    /**
     * 根据被邀请人查找待处理的邀请
     */
    @Query("SELECT gi FROM GroupInvitationEntity gi WHERE gi.inviteeAccount = :inviteeAccount AND gi.invitationStatus = 1 AND (gi.expireTime IS NULL OR gi.expireTime > :currentTime) ORDER BY gi.createDate DESC")
    List<GroupInvitationEntity> findPendingInvitations(@Param("inviteeAccount") String inviteeAccount, @Param("currentTime") Date currentTime);

    /**
     * 根据群ID查找所有邀请记录
     */
    Page<GroupInvitationEntity> findByGroupPaiIdOrderByCreateDateDesc(String groupPaiId, Pageable pageable);

    /**
     * 根据邀请人查找邀请记录
     */
    Page<GroupInvitationEntity> findByInviterAccountOrderByCreateDateDesc(String inviterAccount, Pageable pageable);

    /**
     * 根据群ID和邀请状态查找邀请记录
     */
    List<GroupInvitationEntity> findByGroupPaiIdAndInvitationStatus(String groupPaiId, Integer invitationStatus);

    /**
     * 更新邀请状态
     */
    @Modifying
    @Query("UPDATE GroupInvitationEntity gi SET gi.invitationStatus = :invitationStatus, gi.responseTime = :responseTime, gi.updateDate = :updateDate WHERE gi.id = :id")
    int updateInvitationStatus(@Param("id") Long id, 
                              @Param("invitationStatus") Integer invitationStatus, 
                              @Param("responseTime") Date responseTime, 
                              @Param("updateDate") Date updateDate);

    /**
     * 批量更新过期邀请状态
     */
    @Modifying
    @Query("UPDATE GroupInvitationEntity gi SET gi.invitationStatus = 4, gi.updateDate = :updateDate WHERE gi.invitationStatus = 1 AND gi.expireTime < :currentTime")
    int batchUpdateExpiredInvitations(@Param("currentTime") Date currentTime, @Param("updateDate") Date updateDate);

    /**
     * 统计群邀请数量
     */
    @Query("SELECT COUNT(gi) FROM GroupInvitationEntity gi WHERE gi.groupPaiId = :groupPaiId AND gi.invitationStatus = :invitationStatus")
    Long countByGroupPaiIdAndInvitationStatus(@Param("groupPaiId") String groupPaiId, @Param("invitationStatus") Integer invitationStatus);

    /**
     * 删除群邀请记录（用于解散群聊）
     */
    void deleteByGroupPaiId(String groupPaiId);
}
